package com.hegaoye.base;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
public class RabbitmqConfig {

    public final static String EXCHANGE_NAME = "order.exchange";

    public final static String ORDER_QUEUE_NAME = "order.ordered";
    public final static String ORDER_DISCOUNT_QUEUE_NAME = "order.discount";

    public final static String Test_QUEUE_NAME = "test";
    public final static String Test_EXCHANGE_NAME = "test.exchange";


    @Bean
    public Queue testQueue() {
        return new Queue(Test_QUEUE_NAME);
    }

    @Bean
    public DirectExchange testExchange() {
        return new DirectExchange(Test_EXCHANGE_NAME);
    }

    @Bean(name = "topicExchange")
    public TopicExchange topicExchange() {
        return new TopicExchange(EXCHANGE_NAME);
    }


    @Bean(name = "orderQueue")
    public Queue orderQueue() {
        return new Queue(ORDER_QUEUE_NAME);
    }

    @Bean(name = "orderDiscountQueue")
    public Queue orderDiscountQueue() {
        return new Queue(ORDER_DISCOUNT_QUEUE_NAME);
    }

    @Bean
    public Binding bindingTestQueue(Queue testQueue, DirectExchange testExchange) {
        return BindingBuilder.bind(testQueue).to(testExchange).with(Test_QUEUE_NAME);
    }

    @Bean
    public Binding bindingOrder(@Qualifier("orderQueue") Queue queue, TopicExchange topicExchange) {
        return BindingBuilder.bind(queue).to(topicExchange).with(ORDER_QUEUE_NAME);
    }

    @Bean
    public Binding bindingOrderDiscount(@Qualifier("orderDiscountQueue") Queue queue, TopicExchange topicExchange) {
        return BindingBuilder.bind(queue).to(topicExchange).with(ORDER_DISCOUNT_QUEUE_NAME);
    }

}
